package com.alibaba.citrus.ots.common.util;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @description:
 * @author: yuhw
 * @date: 2021年10月13日 16:36
 **/
public class TimeUtil {

    public static String getYYYYMMddFromTimeFormat(String date){
        if (StringUtils.isBlank(date)){
            return null;
        }
        String[] s = date.split(" ");
        if (s != null && s.length > 0){
            return s[0];
        }
        return  date;

//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    }

    public static String formatYYYYMMdd(Date date){
        if (date == null)return null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String t = sdf.format(date);
        return t;
    }
    public static String formatYYYY_MM_dd(Date date){
        if (date == null)return null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String t = sdf.format(date);
        return t;
    }

    public static Date parseYYYY_MM_dd(String date){
        if (StringUtils.isBlank(date))return null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date parse = sdf.parse(date);
            return parse;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;

    }

    public static String formatYYYY_MM_dd_time(Date date){
        if (date == null)return null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String t = sdf.format(date);
        return t;
    }

    public static Date parseYYYYMMddHHmmss(String date){
        if (StringUtils.isBlank(date)){
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        try{
            return sdf.parse(date);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static Date parseSimpleDateFormat(String date){
        if (StringUtils.isEmpty(date))return null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date parseTime(String date){
        if (StringUtils.isEmpty(date))return null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return sdf.parse(date);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    public static Date parseZeroHour(String date){
        Date time = parseTime(date);
        if (time == null)return null;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        return calendar.getTime();
    }

    /**
     * 获取时间的0点
     * @param date
     * @return
     */
    public static Long parseZero(String date){
        Date time = parseZeroHour(date);
        if (time == null)return null;
        return time.getTime();
    }

    /**
     * 获取到秒
     * @param date
     * @return
     */
    public static Long getTime(String date){
        if (StringUtils.isEmpty(date))return null;
        Date t = parseTime(date);
        if (t != null){
            return t.getTime();
        }
        return null;
    }

    /**
     * 纪元页面传入的日期, 加一天.
     * @param endDate
     * @return
     */
    public static Long getSearchEndTime(String endDate){
        if (StringUtils.isEmpty(endDate)){
            return null;
        }
        Date t = parseTime(endDate);
        if (t != null){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(t);
            calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH) + 1);
            return calendar.getTime().getTime();
        }
        return null;
    }

    /**
     * 时间戳转date
     * @param v
     * @return
     */
    public static Date getDateByLong(Long v){
        if (v == null){
            return null;
        }
        Date date = new Date(v);
        return date;
    }

    public static Date getIgnoreNullDateByLong(Long v){
        if (v == null){
            return null;
        }
        if (v == new Date(0).getTime()){
            return null;
        }
        Date date = new Date(v);
        return date;
    }

    public static String convertYYYYMMdd2YYYYMMddHHmmss(String time){
        if (StringUtils.isBlank(time)){
            return null;
        }
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
            Date date = dateFormat.parse(time);
            return format.format(date);
        }catch (Throwable e){
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(parseSimpleDateFormat("20211014"));
        System.out.println(formatYYYYMMdd(new Date()));
        System.out.println(formatYYYY_MM_dd_time(new Date()));
//        System.out.println(parseTime("2021-11-03"));
        System.out.println(parseZeroHour("2021-10-05 10:33:21"));
        System.out.println(getSearchEndTime("2021-12-03 00:00:00"));;
    }
}
